package com.njsf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.njsf.dao.AdminMapper;
import com.njsf.dto.ResponseJson;
import com.njsf.pojo.Admin;
import com.njsf.service.AdminService;
import com.njsf.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;//;%MAVEN_HOME%\bin;

    @Override
    public ResponseJson checkLogin(String adminName, String pwd, HttpServletRequest request) {
        ResponseJson resutl = null;
        Admin admin = adminMapper.findByNameAndPwd(adminName, MD5Util.MD5(pwd));
        if (admin != null){
            adminMapper.updateLastLoginTime(admin.getId());
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin);
            resutl = new ResponseJson(100,"登录成功!");
        }else{
            resutl = new ResponseJson(101,"登录失败!");
        }
        return resutl;
    }

    @Override
    public PageInfo<Admin> getAdmins(Integer start, Integer pageSize) {
        /*start:查询的第几页  pageSize：每页多少条记录*/
        PageHelper.startPage(start,pageSize);
        List<Admin> all = adminMapper.findAll();
        PageInfo<Admin> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }

    @Override
    public ResponseJson delAdmin(Integer id) {
        ResponseJson result = null;
        int flag = adminMapper.deleteById(id);
        if (flag != -1){
            result = new ResponseJson(100,"删除成功!");
        }else{
            result = new ResponseJson(101,"删除失败!");
        }
        return result;
    }

<<<<<<< HEAD
=======
    /*获取一条管理员信息*/
    @Override
    public Admin getOneAdmin(Integer id) {
        return adminMapper.getOneAdmin(id);
    }

    /*更新用户信息*/
    @Override
    public ResponseJson updateAdmin(Admin admin) {
        ResponseJson result = null;
        admin.setAdminPwd(MD5Util.MD5(admin.getAdminPwd()));
        int flag = adminMapper.updateAdmin(admin);
        if (flag != -1){
            result = new ResponseJson(100,"添加成功!");
        }else{
            result = new ResponseJson(101,"添加失败!");
        }
        return result;
    }

>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8

}
